import comArea from '@/components/common/area.vue'

const area = {
  data() {
    return {
      showArea: false,
      areaSelectValue: [],
      areaName: '选择区域',
      cityName: '选择城市',
    }
  },
  mounted() {
    if (this.$store.getters.location.length > 0) {
      this.cityName = this.$store.getters.location[
        this.$store.getters.location.length - 2
      ].name
      this.areaName = this.$store.getters.location[
        this.$store.getters.location.length - 1
      ].name
      if (this.$route.name == 'Home' || this.$route.name == 'newHome') {
        //首页-加载天气数据
        // this.initData()
      }
    } else {
      this.cityName = '选择区域'
      if (this.$route.name == 'Home' || this.$route.name == 'newHome') {
        //首页-加载地图
        this.initMap()
      }
    }
  },
  components: {
    comArea,
  },
  methods: {
    showAreaMet() {
      this.showArea = true
    },
    closeAreaMet() {
      this.showArea = false
    },
    confirmEmitMet(value) {
      console.log(1111)
      console.log(value)
      this.areaSelectValue = value
      this.$store.dispatch('setLocation', this.areaSelectValue)
      this.cityName = this.areaSelectValue[1].name
      this.areaName = this.areaSelectValue[2].name
      this.showArea = false
      //首页
      if (this.$route.name == 'Home' || this.$route.name == 'newHome') {
        //首页加载数据
        // this.initData()
      }
      //获取当前城市的经纬度
      this.geocodingTask(value)
    },

    toPageHome(cityName, areaName) {
      var areaSelectValue = [{}, { name: cityName }, { name: areaName }]
      this.$store.dispatch('setLocation', areaSelectValue)
      console.log(areaSelectValue)
      console.log(2222)
      this.cityName = cityName
      this.areaName = areaName
      this.$router.push('/')
    },
    geocodingTask(value) {
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0,
      })
      var _this = this
      console.log(value)
      var geocodingTask = null
      SGMap.plugin('SGMap.GeocodingTask').then(function(res) {
        geocodingTask = new SGMap.GeocodingTask()

        geocodingTask
          .getLocation({
            address: `${value[1].name + value[2].name}`,
            region: '山西',
            isEncode: false,
          })
          .then(function(result) {
            console.log(result)

            _this.$store.dispatch('setCityLongAndLati', result.location)

            _this.$toast.clear()
          })
      })
    },
  },
}

export default area
